<?php
        require_once('auth.php');
	require_once 'function.scanDirectories.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<TITLE>My Files</TITLE>
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/REDCAT/jquery-1.3.2.min.js"></script>
</HEAD>

<BODY>

<?php
//Find the current session file
$login = $_SESSION['SESS_LOGIN'];
$cwd = getcwd();
$target = "workspace/".$login;
$file = $_SESSION['SESS_CURR_FILE'];
$dir = dir($target);
?>

<style type="text/css">
tr.filelisting:hover { background-color:#FFC }
tr.filelisting .size { text-align:right }
</style>
<script type="text/javascript">
function deleteFile(name, id) {
	$('#filerow' + id).css('backgroundColor','#FDD');
	if (!confirm('Are you sure you want to delete "' + name +'"?')) {
		// do not delete the file
		$('#filerow' + id).css('backgroundColor','');
		return;
	}
	$.get('deletefile.php', {name: name, id: id});
	$('#filerow' + id).remove();

}
function useFile(name) {
	if(!confirm('Are you sure you want to use "' + name +'"?')) {
		return;
	}
	$.get('usefile.php', {name: name}, function() {
		window.opener.location = 'return-upload.php';
	});
}
</script>
<table width="550px"><tr><th><!-- delete --></th><th>Filename</th><th><!-- use --></th><th width="70px">Size</th><th>Type</th><th>Date Modified</th></tr>

<?php
//Grab the files from the user's directory
$files = scanDirectories($target, false);
foreach ($files as $uid => $f) {
	//Find the size of each file to display
	$size = $f['fileSize'];
	$mod = 'bytes';
	if ($size > 1024) { $size /= 1024; $mod = 'KB'; }
	if ($size > 1024) { $size /= 1024; $mod = 'MB'; }

	//Find the file's extension
	if (strpos($f['fileName'], '.redcat.') !== false) $f['fileExt'] = 'redcat';

	//Display all files with options to delete or download
	echo '<tr class="filelisting" id="filerow'.$uid.'">'."\n";
	echo '	<td><a href="javascript:void(0);" onclick="deleteFile(\''.htmlentities($f['fileName']).'\', '.$uid.')">X</a></td>'."\n"; 
	echo '	<td><a href="downloadfile.php?name='.htmlentities($f['fileName']).'">'.$f['fileName'].'</a></td>'."\n";
	echo ' <td><a href="javascript:void(0);" onclick="useFile(\''.htmlentities($f['fileName']).'\')">Use File</a></td>'."\n";
	echo '	<td class="size">'.(round($size * 10)/10).' '.$mod.'</td>'."\n";
	echo '	<td>'.$f['fileExt'].'</td>'."\n";
	echo '	<td>'.date('D jS, h:m A',$f['fileDate']).'</td>'."\n";
	echo '</tr>'."\n";
}
?>
</table>
</HTML>
